package com.bootdo.friendcircle.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.friendcircle.domain.FriendCircleCommentDO;
import com.bootdo.friendcircle.domain.FriendCircleMessageDO;
import com.bootdo.friendcircle.domain.MessagePicDO;
import com.bootdo.friendcircle.service.FriendCircleCommentService;
import com.bootdo.friendcircle.service.FriendCircleMessageService;
import com.bootdo.friendcircle.service.MessagePicService;
import com.bootdo.system.domain.CompanyPictureDO;
import com.bootdo.system.domain.SowingmapDO;
import com.bootdo.system.domain.WechatPicDO;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;

/**
 * 
 * 
 * @author lzy
 * @email 1992lcg@163.com
 * @date 2018-06-08 17:50:54
 */
 
@Controller
@RequestMapping("/friendcircle/friendCircleMessage")
public class FriendCircleMessageController {
	@Autowired
	private FriendCircleMessageService friendCircleMessageService;
	@Autowired
	private MessagePicService  messagePicService;
	
	@GetMapping()
	String FriendCircleMessage(){
	    return "friendcircle/friendCircleMessage/friendCircleMessage";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<FriendCircleMessageDO> friendCircleMessageList = friendCircleMessageService.list(query);
		for (FriendCircleMessageDO friendCircleMessageDO : friendCircleMessageList) {
			String timeBefore = DateUtils.getTimeBefore(friendCircleMessageDO.getAddTime());
			friendCircleMessageDO.setTimeBefore(timeBefore);
		}
		int total = friendCircleMessageService.count(query);
		
		PageUtils pageUtils = new PageUtils(friendCircleMessageList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "friendcircle/friendCircleMessage/add";
	}

	@GetMapping("/upload")
	String upload(Model model,Integer id){
		model.addAttribute("id", id);
		return "friendcircle/friendCircleMessage/addPic";
	}
	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		FriendCircleMessageDO friendCircleMessage = friendCircleMessageService.get(id);
		model.addAttribute("friendCircleMessage", friendCircleMessage);
	    return "friendcircle/friendCircleMessage/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(String content){
		FriendCircleMessageDO friendCircleMessage=new FriendCircleMessageDO();
		friendCircleMessage.setUserId(ShiroUtils.getUserId());
		friendCircleMessage.setAddTime(new Date());
		friendCircleMessage.setContent(content);
		friendCircleMessage.setLikeCount(0);
		if(friendCircleMessageService.save(friendCircleMessage) > 0){
			return R.ok(""+friendCircleMessage.getId()+"");
		}else {
			return R.error("保存失败");
		}
		
	}
	

	
	
	/**
	 * 保存朋友圈图片
	 */
	@ResponseBody
	@PostMapping("/savePic")
	public R savePic(@RequestParam("img") MultipartFile[] img,Integer id){
		        
				String url =null;
				try {
					 for (MultipartFile file:img) {
						 url = OssUtil.fileUpload(file);
						 MessagePicDO messagePicDO = new MessagePicDO();
						 messagePicDO.setUrl(url); 
						 messagePicDO.setAddTime(new Date());
						 messagePicDO.setFcmId(id);
						 messagePicService.save(messagePicDO);
					 }
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return R.ok("上传成功");
			
	}
	

	
	
	
	/**
	 * 保存朋友圈图片
	 */
	@GetMapping("/toPic")
	public String toPic(){
	
	    return "webuploads/index";
			
	}
	


	
	
	@GetMapping("/uploadpre")
	@ResponseBody
	List<MessagePicDO> uploadpre(@RequestParam Integer id, Model model) {
		Map<String, Object> map = new HashMap<>();
		map.put("fcmId", id);
		List<MessagePicDO> list = messagePicService.list(map);
		return list;
	}
	
	
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( FriendCircleMessageDO friendCircleMessage){
		friendCircleMessageService.update(friendCircleMessage);
		return R.ok();
	}
	
	
	/**
	 * 修改
	 */
	@ResponseBody
	@GetMapping("/updateLikeCount")
	public R updateLikeCount(FriendCircleMessageDO friendCircleMessage){
		 
		Integer id = friendCircleMessage.getId();
		FriendCircleMessageDO friendCircleMessageDO = friendCircleMessageService.get(id);
		friendCircleMessage.setLikeCount(friendCircleMessageDO.getLikeCount()+1);
		
		friendCircleMessageService.update(friendCircleMessage);
		return R.ok();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("friendcircle:friendCircleMessage:remove")
	public R remove( Integer id){		
        Map<String, Object> map = new HashMap<>();
        map.put("fcmId",id);
		List<MessagePicDO> list = messagePicService.list(map);
		for (MessagePicDO messagePicDO : list) {
			String url = messagePicDO.getUrl().substring(45, 62);
			OssUtil.deleteBucketInfo(url);
			messagePicService.remove(messagePicDO.getpId());
		}
		if(friendCircleMessageService.remove(id)>0){
			return R.ok();
		}
			return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("friendcircle:friendCircleMessage:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		friendCircleMessageService.batchRemove(ids);
		return R.ok();
	}
	
}
